package homework;

public class homework01 {
    public static void main(String[] args) {
        Person[] person = new Person[3];
        person[0] = new Person("zhangSan", 10, "JavaEE工程师");
        person[1] = new Person("tom", 20, "大数据工程师");
        person[2] = new Person("marry", 30, "PHP工程师");

        System.out.println("==================排序前================");
        for(int i = 0; i < person.length; i++) {
            System.out.println(person[i]);
        }

        //利用冒泡排序
        Person tmp = null; // 临时变量，用于交换
        for(int i = 0; i < person.length; i++) {
            for(int j = 0; j < person.length - i - 1; j++) {
                //按照age从大到小排序
//                if(person[j + 1].getAge() > person[j].getAge()) {
                //按照名字的长度从小到大
                if(person[j + 1].getName().length() < person[j].getName().length()) {
                    tmp = person[j + 1];
                    person[j + 1] = person[j];
                    person[j] = tmp;
                }
            }
        }
        System.out.println("==================排序后================");
        for(int i = 0; i < person.length; i++) {
            System.out.println(person[i]);
        }
    }
}

class Person {
    private String name;
    private int age;
    private String job;

    public Person(String name, int age, String job) {
        this.name = name;
        this.age = age;
        this.job = job;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }
}